package com.apes.fn.scm.retailOrder.service.retailProcess.process;

import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailOrder.service.retailProcess.CommonRetailDataProcess;
import com.apes.framework.util.MapUtil;
import org.springframework.stereotype.Service;

/**
 * 年卡退回
 *
 * @author wuq
 * @create 2021-05-24 13:43
 */
@Service
public class YearCardReturnProcess extends CommonRetailDataProcess {

    @Override
    public void beforeSave(RetailOrder retailOrder) {
        beforeProcess(retailOrder, (saleOrder -> retailOrder.setScene(null)));
    }

    @Override
    public void afterSave(RetailOrder retailOrder) {
        // 检查是否明细商品全部都为年卡
        long count = retailOrder.getSaleItems().stream().filter(item -> !"70".equals(item.getProduct().getProductGroup().getId())).count();
        if (count > 0) throw new RuntimeException("年卡退款商品明细必须全部为年卡商品！");

        invoke("retail.coupon.returnVoucher", MapUtil.mapped(retailOrder));
    }

    @Override
    public boolean support(String orderType) {
        return "yearCardReturn".equals(orderType);
    }
}
